<?php
/*!
	\file SimpleDBOResultSet
	\brief Contains the class definition for SimpleDBOResultSet
*/

/*!
	\class SimpleDBOResultSet
	\brief A class for iterating through a set of MySQL rows by returning fully-instantiated SimpleDBO objects.
*/
class SimpleDBOResultSet
{
	private $_rs;	//!< Result set ID (MySQL resource ID)
	
	/*!
		\brief Class Constructor
		\param $rs - MySQL Resource ID (returned from a MySQL query that should return multiple rows)
	*/
	public function __construct($rs)
	{
		$this->_rs = $rs;
	}
	
	/*
		\brief Class destructor; clears the result (using SimpleDBOResultSet::Free)
		\returns null
	*/
	public function __destruct()
	{
		$this->Clear();
	}
	
	/*!
		\brief Returns the next row in the result set (as a SimpleDBO instance)
		\param $dataobject - a SimpleDBO instance; data from the next row of results will be loaded into this object
		\returns SimpleDBO instance, or false if there are no more rows in the result set
	*/
	function Next($dataobject)
	{
		// Get the next row of data from MySQL
		$row = SQL::GetObject($this->_rs);
		
		// If the fetch was unsuccessful, we've probably hit the end of our results, so return false
		if(!$row)
			return false;
		
		// Get the names of the columns from the object
		$columns = $dataobject->GetColumnNames();
		
		// Plug them into the data object
		foreach($columns as $column)
			$dataobject->$column = $row->$column;

		return $dataobject;
	}
	
	/*!
		\brief Returns all rows in the result set as an array of SimpleDBO instances
		\param $dataobject - SimpleDBO instance; array values will be objects of this type
		\returns SimpleDBO instance, or false if there are no more rows in the result set
	*/
	function All($dataobject)
	{
		// Prep
		$all = array();
		$columns = $dataobject->GetColumnNames();
		
		// Loop through the rows in the result set
		while($row = SQL::GetObject($this->_rs))
		{
			// Create a new instance, fill him with data from the database, add him to our array
			$newobj = SimpleDBOFactory::Create($dataobject->_dbo_table);
			foreach($columns as $column)
				$newobj->$column = $row->$column;
			$all[] = $newobj;
		}
		return $all;
	}
	
	/*!
		\brief Resets the result set pointer back to the first row
		\returns nothing
	*/
	function Reset()
	{
		SQL::Seek(0, $this->_rs);
	}
	
	/*!
		\brief Retrieves the number of rows in the current result set
		\returns int
	*/
	function NumRows()
	{
		return SQL::NumRows($this->_rs);
	}
	
	/*!
		\brief Clears the result set using SQL::Free
		\returns Whatever SQL::Free returns
	*/
	function Clear()
	{
		return SQL::Free($this->_rs);
	}
}

?>